<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-9-18
 * Time: 下午4:57
 */
class PriceController extends Controller {

    private $_title;

    public function init() {
        parent::init();
    }

    public function indexAction(){
        define('ITEM_PER_PAGE', 20);

        $region = $this->_request->getParam('region');
        $region=empty($region) ? '全部' :htmlspecialchars(trim($region) , ENT_QUOTES ,'utf-8');
        $brand_name= $this->_request->getParam('brand_name');
        $brand_name=empty($brand_name) ? '' :htmlspecialchars(trim($brand_name) , ENT_QUOTES ,'utf-8');
        $cn= $this->_request->getParam('cn');
        $cn=empty($cn) ? '' :htmlspecialchars(trim($cn) , ENT_QUOTES ,'utf-8');
        $area= $this->_request->getParam('area');
        $area=empty($area) ? '' :htmlspecialchars(trim($area) , ENT_QUOTES ,'utf-8');
        $keyword= $this->_request->getParam('keyword');
        $keyword=empty($keyword) ? '' :htmlspecialchars(trim($keyword) , ENT_QUOTES ,'utf-8');
        $orderbyNum= $this->_request->getParam('orderbyNum','ASC');
        $orderbyNum=empty($orderbyNum) ? '' :htmlspecialchars(trim($orderbyNum) , ENT_QUOTES ,'utf-8');
        $orderByPrice= $this->_request->getParam('orderByPrice','ASC');
        $orderByPrice=empty($orderByPrice) ? '' :htmlspecialchars(trim($orderByPrice) , ENT_QUOTES ,'utf-8');
        $minPrice= $this->_request->getParam('minPrice');
        $minPrice=empty($minPrice) ? '' :htmlspecialchars(trim($minPrice) , ENT_QUOTES ,'utf-8');
        $maxPrice= $this->_request->getParam('maxPrice');
        $maxPrice=empty($maxPrice) ? '' :htmlspecialchars(trim($maxPrice) , ENT_QUOTES ,'utf-8');
        $type= $this->_request->getParam('type');
        $type=empty($type) ? '' :htmlspecialchars(trim($type) , ENT_QUOTES ,'utf-8');
        $currency_code= $this->_request->getParam('currency_code');
        $currency_code=empty($currency_code) ? 'RMB' :htmlspecialchars(trim($currency_code) , ENT_QUOTES ,'utf-8');
        $page= $this->_request->getParam('page');
        $page  = empty($page) ? 1 : (int) $page;

        $pageurl = "/price/index";


        $val =array(
            'region'  => $region,
            'currency_code' => $currency_code,
            'brand_name'  => $brand_name,
            'product' => $brand_name,
            'cate_name' => $cn,
            'area'     => $area,
            'keyword' => $keyword,
            'pageSize'   => ITEM_PER_PAGE,
            'page'   => $page,
            'type'   => $type,
            'orderByNum'=>$orderbyNum,
            'orderByPrice'=>$orderByPrice,
            'minPrice'=>$minPrice,
            'maxPrice'=>$maxPrice,
        );
        $url = _HTTP_CLOUD_HOST."/api/getprice/getprice";

        $pro = Tools::https_request($url,$val);
        $prolist = json_decode($pro,true);

        $totalItems   = $prolist['totalSize'];
        $offset = 0;
        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems)
        {
            $page   = floor($totalItems / ITEM_PER_PAGE);
            $offset         = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0)
        {
            $offset         = 0;
            $page   = 1;
        }


        foreach($prolist['data'] as $key=>$var){
            $prolist['data'][$key]['create_time'] =  Tools::time2Units(time() - strtotime($var['create_time'])) . "前";
        }


        $getlists = $prolist['data'];


        if(!empty($page)){
            $pageurl .= "/page/%d";
        }
        if(!empty($region)){
            $pageurl .= "/region/$region";
        }
        if(!empty($cn)){
            $pageurl .= "/cn/$cn";
        }
        if(!empty($currency_code)){
            $pageurl .= "/currency_code/$currency_code";
        }
        if(!empty($brand_name)){
            $pageurl .= "/brand_name/$brand_name";
        }
        if(!empty($keyword)){
            $pageurl .= "/keyword/$keyword";
        }


        $links = Tools::createPage($pageurl, ITEM_PER_PAGE, $page, $totalItems, $target = '_self');

        $this->_view->links = $links;

        $this->_view->keyword = $keyword;
        $this->_view->totalItems = $totalItems;
        $this->_view->page =$page;
        $this->_view->getlists =$getlists;
        $this->_view->orderbyNum = $orderbyNum;
        $this->_view->orderByPrice = $orderByPrice;

    }

    public function detailAction(){
        $this->_title ="牌号详情";

        $id = $this->_request->getParam('id');//获取url的参数
        if(empty($id)){
            $this->redirect('/index');
            die;
        }
        $validate =array(
            'id'  =>  $id,
        );

        $url = _HTTP_CLOUD_HOST."/api/getprice/getQuoteById";

        $prodcut = Tools::https_request($url,$validate);
        $getlist = json_decode($prodcut,true);
        $getlist['data']['create_time'] = Tools::time2Units(time() - strtotime($getlist['data']['create_time'])) . "前";


        $this->_view->title = $this->_title;
        $this->_view->getlist = $getlist['data'];
    }

    public function selectAction(){
        //筛选条件有 品种，厂商，地区，币种
        global $_cateArr;
        $this->_title ="筛选";

        $url = _HTTP_CLOUD_HOST."/api/getprice/getprice";
        $validates =array(
            'pageSize'   => 100,
            'page'   => 1,
        );
        $prodcuts = Tools::https_request($url,$validates);
        $getlists = json_decode($prodcuts,true);


        $brand_name = array();
        if(!empty($getlists['data'])){
            foreach($getlists['data'] as $row){
                $brand_name[]= $row['brand_name'];
            }
        }
        $brand_name = array_unique($brand_name);///去除重复的


        $this->_view->_cateArr = $_cateArr;
        $this->_view->title = $this->_title;
        $this->_view->brand_name = $brand_name;
    }


    public function shoplistAction(){


        define('ITEM_PER_PAGE', 20);
        $shopid = $this->_request->getParam('shopid');//获取url的参数
        $shopid=empty($shopid) ? 0 :(int) trim($shopid) ;
        $area_id = $this->_request->getParam('area_id');//获取url的参数
        $area_id=empty($area_id) ? 0 :(int) trim($area_id) ;
        $region = $this->_request->getParam('region');
        $region=empty($region) ? '全部' :htmlspecialchars(trim($region) , ENT_QUOTES ,'utf-8');
        $brand_name= $this->_request->getParam('brand_name');
        $brand_name=empty($brand_name) ? '' :htmlspecialchars(trim($brand_name) , ENT_QUOTES ,'utf-8');
        $cn= $this->_request->getParam('cn');
        $cn=empty($cn) ? '' :htmlspecialchars(trim($cn) , ENT_QUOTES ,'utf-8');
        $area= $this->_request->getParam('area');
        $area=empty($area) ? '' :htmlspecialchars(trim($area) , ENT_QUOTES ,'utf-8');
        $keyword= $this->_request->getParam('keyword');
        $keyword=empty($keyword) ? '' :htmlspecialchars(trim($keyword) , ENT_QUOTES ,'utf-8');
        $orderbyNum= $this->_request->getParam('orderbyNum','ASC');
        $orderbyNum=empty($orderbyNum) ? '' :htmlspecialchars(trim($orderbyNum) , ENT_QUOTES ,'utf-8');
        $orderByPrice= $this->_request->getParam('orderByPrice','ASC');
        $orderByPrice=empty($orderByPrice) ? '' :htmlspecialchars(trim($orderByPrice) , ENT_QUOTES ,'utf-8');
        $minPrice= $this->_request->getParam('minPrice');
        $minPrice=empty($minPrice) ? '' :htmlspecialchars(trim($minPrice) , ENT_QUOTES ,'utf-8');
        $maxPrice= $this->_request->getParam('maxPrice');
        $maxPrice=empty($maxPrice) ? '' :htmlspecialchars(trim($maxPrice) , ENT_QUOTES ,'utf-8');
        $type= $this->_request->getParam('type');
        $type=empty($type) ? '' :htmlspecialchars(trim($type) , ENT_QUOTES ,'utf-8');
        $currency_code= $this->_request->getParam('currency_code');
        $currency_code=empty($currency_code) ? 'RMB' :htmlspecialchars(trim($currency_code) , ENT_QUOTES ,'utf-8');
        $page= $this->_request->getParam('page');
        $page  = empty($page) ? 1 : (int) $page;

        $pageurl = "/price/shoplist";

        if(!empty($shopid)){
            $pageurl .= "/shopid/".$shopid;
        }


        $val =array(
            'region'  => $region,
            'currency_code' => $currency_code,
            'brand_name'  => $brand_name,
            'product' => $brand_name,
            'organization_id' =>$shopid,
            'cate_name' => $cn,
            'area'     => $area,
            'area_id'     => $area_id,
            'keyword' => $keyword,
            'pageSize'   => ITEM_PER_PAGE,
            'page'   => $page,
            'type'   => $type,
            'orderByNum'=>$orderbyNum,
            'orderByPrice'=>$orderByPrice,
            'minPrice'=>$minPrice,
            'maxPrice'=>$maxPrice,
        );
        $url = _HTTP_CLOUD_HOST."/api/getprice/getprice";

        $pro = Tools::https_request($url,$val);
        $prolist = json_decode($pro,true);

        $totalItems   = $prolist['totalSize'];
        $offset = 0;
        $offset = ($page - 1) * ITEM_PER_PAGE;
        if ($offset >= $totalItems)
        {
            $page   = floor($totalItems / ITEM_PER_PAGE);
            $offset         = ($page - 1) * ITEM_PER_PAGE;
        }
        if ($offset < 0)
        {
            $offset         = 0;
            $page   = 1;
        }


        foreach($prolist['data'] as $key=>$var){
            $prolist['data'][$key]['create_time'] =  Tools::time2Units(time() - strtotime($var['create_time'])) . "前";
        }


        $getlists = $prolist['data'];


        $shopinfo =array();

        //店铺联系人信息
        foreach($getlists as $var){
            if(isset($shopinfo)){
                $shopinfo['short_name'] = $var['short_name'];
                $shopinfo['realname'] = $var['realname'];
                $shopinfo['qq'] = $var['qq'];
                $shopinfo['mobile'] = $var['mobile'];
            }else{
                continue;
            }
        }


        if(!empty($page)){
            $pageurl .= "/page/%d";
        }
        if(!empty($region)){
            $pageurl .= "/region/$region";
        }
        if(!empty($cn)){
            $pageurl .= "/cn/$cn";
        }
        if(!empty($currency_code)){
            $pageurl .= "/currency_code/$currency_code";
        }
        if(!empty($brand_name)){
            $pageurl .= "/brand_name/$brand_name";
        }
        if(!empty($keyword)){
            $pageurl .= "/keyword/$keyword";
        }

        $links = Tools::createPage($pageurl, ITEM_PER_PAGE, $page, $totalItems, $target = '_self');



        $this->_view->title =$shopinfo['short_name'];
        $this->_view->links = $links;
        $this->_view->shopinfo = $shopinfo;
        $this->_view->totalItems = $totalItems;
        $this->_view->page =$page;
        $this->_view->getlists =$getlists;
        $this->_view->orderbyNum = $orderbyNum;
        $this->_view->orderByPrice = $orderByPrice;

    }

}